// store/tabStore.ts
import { ref } from 'vue'
import { defineStore } from 'pinia'

interface NavOption {
    name: string;
    // 可以根据需要添加其他属性
}

export const useTabStore = defineStore('tab', () => {
    const navs = ref<NavOption[]>([])
    const index = ref(0)

    const changeIndex = (current: number) => {
        index.value = current
    }

    const add = (option: NavOption) => {
        const currentIndex = navs.value.findIndex(item => item.name === option.name)
        
        if (currentIndex === -1) {
            navs.value.push(option)
            index.value = navs.value.length - 1
        } else {
            changeIndex(currentIndex)
        }
    }

    const remove = (i: number) => {
        if (i >= 0 && i < navs.value.length) {
            navs.value.splice(i, 1)
            // 更新 index 以确保它在有效范围内
            index.value = Math.max(0, index.value - 1)
        }
    }

    const empty = () => {
        navs.value = []
    }

    return {
        navs,
        index,
        add,
        remove,
        changeIndex,
        empty
    }
})